<?php

namespace app\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Equiptype;

/**
 * EquiptypeSearch represents the model behind the search form about `app\models\Equiptype`.
 */
class EquiptypeSearch extends Equiptype
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'value_type'], 'integer'],
            [['code', 'name_ch', 'name_en', 'image', 'comment', 'value_unit'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Equiptype::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'value_type' => $this->value_type,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'name_ch', $this->name_ch])
            ->andFilterWhere(['like', 'name_en', $this->name_en])
            ->andFilterWhere(['like', 'image', $this->image])
            ->andFilterWhere(['like', 'comment', $this->comment])
            ->andFilterWhere(['like', 'value_unit', $this->value_unit]);

        return $dataProvider;
    }
}
